<?php
    function gold_get_post_categotyies() {
        $categotyies_array = array();
         
        $terms_categories = get_terms('category', array('term_id','name','slug'));    
        if ( !empty( $terms_categories ) ) {
            foreach ( $terms_categories as $term_category ) {
                
                $keyterm_slug = $term_category->slug;
                $keyterm_name = $term_category->name;
                $keyterm_id = $term_category->term_id;
        
                $term_num_posts = sizeof( query_posts(array( 'post_type'=>'post','taxonomy'=>'category','term'=>$keyterm_slug)) );
                    if ( !empty( $term_num_posts ) ) {
                            $keyterm_name = $keyterm_name.' ('.$term_num_posts.')';
                        }
                $categotyies_array[$keyterm_slug] = $keyterm_name;
                wp_reset_query();
            }   
        }
        return $categotyies_array;
    }
 
    function gold_posts_categories() {
    	register_widget('gold_show_posts_categories');
    }
    
class  gold_show_posts_categories extends WP_Widget {
    function gold_show_posts_categories() {
    global $themename;
        $widget_ops = array('description' => __('Show featured images of posts from this category', '') );
    	parent::WP_Widget(false, $name=$themename.' - Show posts from category', $widget_ops);
    }
    
    /**
     * Displays  post widget on blog.
     */
    function widget($args, $instance) {
    	extract( $args );
        global $page_id_num1;
        $is_post_active = false;
        $post_num = 0;
        $postcategories = $instance["postcategories"];
       
        echo $before_widget;
        if ($postcategories!="") {
            $term_num_posts = sizeof(query_posts(array('posts_per_page'=>-1, 'orderby'=>'menu_order date','post_type'=>'post','taxonomy'=>'category','term'=>$postcategories)));
        }elseif ($postcategories==""){
            $term_num_posts = sizeof(query_posts(array('posts_per_page'=>-1, 'orderby'=>'menu_order date','post_type'=>'post','taxonomy'=>'category')));
        }
            
            if ( $term_num_posts>7){
                $ul_size = 142*$term_num_posts;
                }else{ $ul_size =1025;}
           // echo $page_id_num1;
           // die('test here');
        echo '<ul class="slideImg" style="width:'.$ul_size.'px;">';
        $count_post = 0;
            if (have_posts()) : while (have_posts()): the_post();
                $post_id_num1 = get_the_ID();
                if($page_id_num1 == $post_id_num1) { 
                       $is_post_active = true;
                       $post_num = $count_post;
                }
                echo '<li id="';
                echo 'fade-image_'.$count_post;
                echo'">';
                echo '<a class="fade-image ';
                if($page_id_num1 == $post_id_num1) { 
                       echo  'fade-image-active';
                    }
                echo '" href="'.get_permalink().'" >';
                if ( function_exists('has_post_thumbnail') && has_post_thumbnail()){
                    $thumb_id = get_post_thumbnail_id( $post_id_num1 );
                    $image_thumbnail = wp_get_attachment_image_src( $thumb_id,'full' ); 
                    echo '<img class="fade-image-a" width="140" height="100" alt="" src="';
                    echo   get_bloginfo('template_url').'/lib/functions/timthumb/timthumb.php?src='.$image_thumbnail[0].'&w=140&h=100&s=1"/>';
                    }
                if ( (class_exists('MultiPostThumbnails')) && MultiPostThumbnails::has_post_thumbnail('post', 'second-featured-image')) {
                    $image_thumbnail3 = MultiPostThumbnails::get_the_post_thumbnail_link('post', 'second-featured-image');
                    echo '<img class="fade-image-b" width="140" height="100" alt="" src="';
                    echo   get_bloginfo('template_url').'/lib/functions/timthumb/timthumb.php?src='.$image_thumbnail3.'&w=140&h=100&s=1"/>';
                    }
                echo '</a>'; 
                echo '</li>';
                $count_post++;
                endwhile; 
            else: 
                echo __('');
            endif;
            echo '</ul>';
            wp_reset_query(); 
        
      echo $after_widget;
      if ($is_post_active == true){
                echo '<script type="text/javascript">'; 
                    echo '$(document).ready(function(){';
                    echo '';
                    echo 'var slide_imgContent_pane = $(".slide_imgContent");';
                	echo 'slide_imgContent_pane.jScrollPane({';
                	  echo 'horizontalDragMinWidth: 77,';
                	  echo 'horizontalDragMaxWidth: 77,';
                      echo 'animateScroll: true';
                	echo '});';
                    echo 'var api_slide = slide_imgContent_pane.data("jsp");';
                 //   $post_1 = (1024/$term_num_posts);
                //    $position_x = round(($post_num*$post_1)+($post_1/2));
               // $position_x = round($post_num*$post_1);
                 //   echo 'alert("'.$position_x.'");';
                //  echo 'api_slide.scrollToX('.$position_x.', true);';
                 // echo 'slide_imgContent_pane[0].scrollTo(0,"#fade-image_'.$post_num.'",true);';
                 if($post_num==0){
                    $percentXb=0;
                 }elseif(($post_num+1)==$term_num_posts){
                    $percentXb=100;
                    }else{
                       $percentXb = (($post_num/$term_num_posts) + (1/$term_num_posts)/2);
                    }
                    echo 'api_slide.scrollToPercentX('.$percentXb.',false);';
                  //  echo 'alert('.$percentXb.');';
    
                    echo '});';
                echo '</script>';
            }
    			
    }
    /**
     * Form processing....
     */
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['postcategories'] =  $new_instance['postcategories'] ; 
       	return $instance;
    }
    /**
     *  form.
     */
    function form($instance) {
    ?>
    <p>
        <label>Show featured images of posts from this category.</label>
    </p>
    <?php $all_categories = gold_get_post_categotyies(); ?>
    <p>
        <label for="<?php echo $this->get_field_id("postcategories"); ?>">
        <?php _e( 'Choose Category : ','cfma'); ?>
        <select name="<?php echo $this->get_field_name("postcategories"); ?>" id="<?php echo $this->get_field_id("postcategories"); ?>"  style="width: 95%;" >
        echo '<option value="">All</option>';
        <?php
        	foreach ($all_categories as $key => $value){
                  echo '<option value="'.$key.'"',$instance["postcategories"] == $key ?' selected="selected"' : '' ,'>'.$value.'</option>';
        	}
        ?>
        </select>
        </label>
    </p>
    
    <?php
    }
}
add_action( 'widgets_init', 'gold_posts_categories' );
?>